package com.jf.dao.impl;

import com.jf.bean.Project2;
import com.jf.dao.Project2Dao;
import com.jf.util.BaseDao;
import com.jf.util.MyUtils;
import com.jf.util.PageInfo;

import java.util.ArrayList;
import java.util.List;

public class Project2DaoImpl implements Project2Dao {
    @Override
    public List<Project2> getPage(PageInfo pageInfo, Project2 project2) {
        String sql = "select * from project where 1 = 1 ";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(project2.getName())){
            sql+="and name like concat('%',?,'%') ";
            objs.add(project2.getName());
        }
        /*此处id的值为随机生成的uuid，因此这里对id倒序排序，结果不一定是最新的数据在最前面*/
        sql+="order by id desc limit ?,?";
        objs.add(pageInfo.getOffset());
        objs.add(pageInfo.getPageSize());
        return BaseDao.baseQuery(Project2.class,sql,objs);
    }

    @Override
    public int getCount(Project2 project2) {
        String sql = "select count(id) from project where 1 = 1 ";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(project2.getName())){
            sql+="and name like concat('%',?,'%') ";
            objs.add(project2.getName());
        }
        return BaseDao.baseQueryCount(sql,objs);
    }

    @Override
    public Project2 getById(String id) {
        return BaseDao.baseQueryBean(Project2.class,"select * from project where id = ?",id);
    }

    @Override
    public int updateSelect(Project2 project2) {
        List<Object> objs = new ArrayList<>();
        String sql = "update project set ";
        if(MyUtils.isStringNotEmpty(project2.getName())){
            sql+="name = ?,";
            objs.add(project2.getName());
        }
        if(MyUtils.isStringNotEmpty(project2.getProfile())){
            sql+="profile = ?,";
            objs.add(project2.getProfile());
        }
        if(MyUtils.isStringNotEmpty(project2.getSrc())){
            sql+="src = ?,";
            objs.add(project2.getSrc());
        }
        sql = sql.substring(0,sql.lastIndexOf(","));
        sql+=" where id = ?";
        objs.add(project2.getId());
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int addSelect(Project2 project2) {
        List<Object> objs = new ArrayList<>();
        String sql = "insert into project (";
        if(MyUtils.isStringNotEmpty(project2.getId())){
            sql+="id,";
        }
        if(MyUtils.isStringNotEmpty(project2.getName())){
            sql+="name,";
        }
        if(MyUtils.isStringNotEmpty(project2.getProfile())){
            sql+="profile,";
        }
        if(MyUtils.isStringNotEmpty(project2.getSrc())){
            sql+="src,";
        }
        sql = sql.substring(0,sql.lastIndexOf(","));
        sql+=") values(";
        if(MyUtils.isStringNotEmpty(project2.getId())){
            sql+="?,";
            objs.add(project2.getId());
        }
        if(MyUtils.isStringNotEmpty(project2.getName())){
            sql+="?,";
            objs.add(project2.getName());
        }
        if(MyUtils.isStringNotEmpty(project2.getProfile())){
            sql+="?,";
            objs.add(project2.getProfile());
        }
        if(MyUtils.isStringNotEmpty(project2.getSrc())){
            sql+="?,";
            objs.add(project2.getSrc());
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=")";
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int batchDelete(String[] ids) {
        List<Object> objs = new ArrayList<>();
        StringBuilder sql = new StringBuilder("delete from project where id in (");
        for(String id : ids){
            sql.append("?,");
            objs.add(id);
        }
        sql = new StringBuilder(sql.substring(0,sql.lastIndexOf(",")));
        sql.append(")");
        return BaseDao.baseUpdate(sql.toString(),objs);
    }

    @Override
    public List<Project2> getList() {
        String sql = "select * from project order by name";
        return BaseDao.baseQuery(Project2.class,sql);
    }
}
